//
//  MyStockFundFlowController.m
//  Stock
//
//  Created by blacksun on 11-7-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStockFundFlowController.h"
#import "BarChartViewController.h"


@implementation MyStockFundFlowController
@synthesize label10;
@synthesize label11;
@synthesize label12;
@synthesize label13;
@synthesize label14;
@synthesize label15;
@synthesize fundFlow;
@synthesize fundFlowImageView;
@synthesize ftUpdateTime;
@synthesize fcLoadingView;
@synthesize flowChartController;
@synthesize fcUpdateTime;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    NSString *action = [aUserinfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([action isEqualToString:kMystockServiceQueryFundFlow]) {
        self.fundFlow = aResult;
        myStock.ff = updateTime;
        flowChartController.fundFlow = aResult;
        [flowChartController loadChartData];
    }else if([action isEqualToString:kMyStockServiceQueryForceTrace]){
        self.forceTrace = aResult;
        myStock.ft = ftUpdateTime;
    }
    [self reloadViews];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}
-(void)reloadViews{
    UIColor *greenColor = [UIColor greenColor];
    if ([fundFlow.fIn hasPrefix:@"-"]) {
        label1.textColor = greenColor;
    }
    if ([fundFlow.fOut hasPrefix:@"-"]) {
        label2.textColor = greenColor;
    }
    if ([fundFlow.ft hasPrefix:@"-"]) {
        label4.textColor = greenColor;
    }
    if ([fundFlow.f0 hasPrefix:@"-"]) {
        label5.textColor = greenColor;
        //label7.textColor = greenColor;
    }
    if ([fundFlow.f3 hasPrefix:@"-"]) {
        label6.textColor = greenColor;
        //label10.textColor = greenColor;
    }
    if ([fundFlow.f1 hasPrefix:@"-"]) {
        //label8.textColor = greenColor;
    }
    if ([fundFlow.f2 hasPrefix:@"-"]) {
        //label9.textColor = greenColor;
    }
    label1.text = fundFlow.fIn;
    label2.text = fundFlow.fOut;
    label3.text = fundFlow.turnover;
    label4.text = fundFlow.ft;
    label5.text = fundFlow.f0;
    label6.text = fundFlow.f3;
    
    
    UIColor *color3 = [UIColor redColor];
    UIColor *color10 = [UIColor redColor];
    UIColor *color60 = [UIColor redColor];
    if ([forceTrace.f3 hasPrefix:@"-"]) {
        color3 = [UIColor greenColor];
    }
    if ([forceTrace.f10 hasPrefix:@"-"]) {
        color10 = [UIColor greenColor];
    }
    if ([forceTrace.f60 hasPrefix:@"-"]) {
        color60 = [UIColor greenColor];
    }
    label7.textColor = color3;
    label8.textColor = color3;
    //label3.textColor = color3;
    label10.textColor = color10;
    label11.textColor = color10;
    //label6.textColor = color10;
    label13.textColor = color60;
    label14.textColor = color60;
    //label9.textColor = color60;
    self.label7.text = forceTrace.f3;
    self.label10.text = forceTrace.f10;
    self.label13.text = forceTrace.f60;
    self.label9.text = forceTrace.turnover3;
    self.label12.text = forceTrace.turnover10;
    self.label15.text = forceTrace.turnover60;
    self.label8.text = forceTrace.fr3;
    self.label11.text = forceTrace.fr10;
    self.label14.text = forceTrace.fr60;
    
}


- (void)dealloc
{
    self.label10 = nil;
    self.label11 = nil;
    self.label12 = nil;
    self.label13 = nil;
    self.label14 = nil;
    self.label15 = nil;
    self.fundFlow = nil;
    self.fundFlowImageView = nil;
    self.ftUpdateTime = nil;
    self.fcLoadingView = nil;
    self.flowChartController = nil;
    self.fcUpdateTime = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(void)updatePubTime{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[parentController.myStock.ff longLongValue]/1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.pubDateLabel.text = [dateFormatter stringFromDate:date];
    [date release];
    [dateFormatter release];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self performSelector:@selector(updateParent) withObject:nil afterDelay:0.1];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePubTime) name:kUpdateStockNotification object:nil];
    
}
-(void)setupSelf{
    self.fundFlow = [MyStockFundFlow findByStockId:myStock.uuid];
    self.forceTrace = [MyStockForceTrace findByStockId:myStock.uuid];
    MyStock *remoteMyStock = [[StockContext shareInstance] findRemoteStockWithStockId:myStock.uuid];
    self.updateTime = remoteMyStock.ff;
    
    if (!fundFlow || ![updateTime isEqualToString:myStock.ff]) {
        [myStockService queryStockFundFlowWithStockId:myStock.uuid];
    }
    self.ftUpdateTime = remoteMyStock.ft;
    if (!forceTrace ||![ftUpdateTime isEqualToString:myStock.ft]) {
        [myStockService queryStockForceTraceWithStockId:myStock.uuid];
    }
    chartFrame = CGRectMake(0.0, 197, 320.0, 120.0);
    
    BarChartViewController *dayController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    dayController.stockId = myStock.uuid;
    dayController.chartType = @"d";
    dayController.serviceType = @"ftc";
    dayController.view.frame = chartFrame;
    BarChartViewController *weekController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    weekController.stockId = myStock.uuid;
    weekController.chartType = @"w";
    weekController.serviceType = @"ftc";
    
    BarChartViewController *monthController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    monthController.stockId = myStock.uuid;
    monthController.chartType = @"m";
    monthController.serviceType = @"ftc";
    
    
    [self.view addSubview:dayController.view];
    
    [self.childViewControllers addObject:dayController];
    [self.childViewControllers addObject:weekController];
    [self.childViewControllers addObject:monthController];
    [dayController release];
    [weekController release];
    [monthController release];
}
-(void)updateParent{
    [parentController childViewDidUpdated:self.view.frame.size.height];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    self.label10 = nil;
    self.label11 = nil;
    self.label12 = nil;
    self.label13 = nil;
    self.label14 = nil;
    self.label15 = nil;
    self.fundFlow = nil;
    self.fundFlowImageView = nil;
    self.ftUpdateTime = nil;
    self.fcLoadingView = nil;
    self.flowChartController = nil;
    self.fcUpdateTime = nil;
}
@end
